package com.sabm.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sabm.dao.ContactDAOImpl;
import com.sabm.dao.UserDAOImpl;
import com.sabm.model.Account;
import com.sabm.model.Contact;
import com.sabm.model.ContactPaging;
import com.sabm.service.DatabaseService;

//author Phu Ta
@Controller
public class ContactController {
	private static final int NUMBER_PAGING = 20;
	@Autowired
	private DatabaseService dbs;

	// load list all of contacts of current user
	@RequestMapping(value = { "/listContacts.do" })
	public @ResponseBody
	ContactPaging listContacts(ModelMap model,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
		int start = page * NUMBER_PAGING - NUMBER_PAGING;
		Account account = (Account) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		List<Contact> contactList = dbs.getContactsByAccount(account
				.getIdAccount(), start, NUMBER_PAGING);
		int totalPages = (int)Math.ceil((double)dbs.getTotalContactsByAccount(account.getIdAccount())/NUMBER_PAGING);
		ContactPaging contactPaging = new ContactPaging(contactList, totalPages, page);

		return contactPaging;
	}

	// search contacts of current user
	@RequestMapping(value = { "/searchContact.do" }, method = RequestMethod.POST)
	public @ResponseBody
	ContactPaging searchContact(ModelMap model,
			@RequestParam(value = "searchkey", required = true) String searchKey) {
		List<Contact> resultList1 = new ArrayList<Contact>();
		List<Contact> resultList2 = new ArrayList<Contact>();
		List<Contact> resultList3 = new ArrayList<Contact>();
		Account account = (Account) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		List<Contact> contactList = dbs.getContactsByAccount(account
				.getIdAccount());
		String key = searchKey.toLowerCase();
		List<String> keyArray = Arrays.asList(key.split("\\s+"));
		//
		for (Contact ct : contactList) {
			String fullName = ct.getFirstName().toLowerCase() + " "
					+ ct.getLastName().toLowerCase();
			if (ct.getFirstName().equalsIgnoreCase(key)) {
				resultList1.add(ct);
			} else if (fullName.contains(key)) {
				resultList2.add(ct);
			} else {
				String[] words = fullName.split("\\s+");
				if (Arrays.asList(words).containsAll(keyArray)) {
					resultList3.add(ct);
				}
			}
		}
		resultList1.addAll(resultList2);
		resultList1.addAll(resultList3);
		ContactPaging contactPaging = new ContactPaging(resultList1, 1, 1);
		return contactPaging;
	}

	//
	@RequestMapping(value = { "/updateContact.do" }, method = RequestMethod.POST)
	public @ResponseBody
	Contact updateContact(@RequestBody Contact contact) {
		Contact resultContact = null;
		Account account = (Account) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		contact.setAccount(account);
		try {
			dbs.updateContact(contact);
			resultContact = dbs.getContactById(contact.getIdContact());
		} catch (Exception e) {
		}
		return resultContact;
	}

	@RequestMapping(value = { "/addContact.do" }, method = RequestMethod.POST)
	public @ResponseBody
	String addContact(@RequestBody Contact contact) {
		String result = "Add contact successfully!";
		Account account = (Account) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		contact.setAccount(account);
		try {
			dbs.addContact(contact);
		} catch (Exception e) {
			result = "error";
		}
		return result;
	}

	@RequestMapping(value = { "/deleteContact.do" }, method = RequestMethod.POST)
	public @ResponseBody
	String deleteContact(@RequestBody Contact contact) {
		String result = "Deleted!";
		Account account = (Account) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		contact.setAccount(account);
		try {
			dbs.deleteContact(contact);
		} catch (Exception e) {
			result = "error";
		}
		return result;
	}
}
